<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');

class Edit extends Layout {

    protected $objAlert, $mctService;
    protected $txtName, $txtDescription, $chkIsEnable, $txtOrder;
    protected $btnSave;

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/service/menu/edit.tpl.php';

        $this->CssClass = 'form-horizontal';

        $this->objAlert = new Alert($this);

        $this->mctService = ServiceMetaControl::CreateFromQueryString($this, QMetaControlCreateType::CreateOrEdit);

        $this->txtName = $this->mctService->txtName_Create();

        $this->txtDescription = $this->mctService->txtDescription_Create();
        $this->txtDescription->Rows = 8;

        $this->txtDescription->AddCssFile('../../../admin/content/plugins/bootstrap-wysihtml5/src/bootstrap-wysihtml5.css');
        $this->txtDescription->AddJavascriptFile('../../../admin/content/plugins/bootstrap-wysihtml5/lib/js/wysihtml5-0.3.0.js');
        $this->txtDescription->AddJavascriptFile('../../../admin/content/plugins/bootstrap-wysihtml5/src/bootstrap-wysihtml5.js');

        $this->chkIsEnable = $this->mctService->chkIsEnable_Create();
        $this->chkIsEnable->AddCssFile('../../../admin/content/plugins/jquery-prettyCheckable/dist/prettyCheckable.css');
        $this->chkIsEnable->AddJavascriptFile('../../../admin/content/plugins/jquery-prettyCheckable/dist/prettyCheckable.min.js');

        $this->txtOrder = $this->mctService->txtOrder_Create();

        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save Changes');
        $this->btnSave->PrimaryButton = $this->btnSave->CausesValidation = true;
        $this->btnSave->AddAction(new QClickEvent(), new QToggleEnableAction($this->btnSave));
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));

        $this->btnSave->AddJavascriptFile('../../../admin/content/js/service/edit.js');
    }
    
    protected function Form_PreRender() {
        parent::Form_PreRender();
        
        QApplication::ExecuteJavaScript('Edit.init();');
    }

    protected function Form_Validate() {
        $blnToReturn = parent::Form_Validate();

        if ($blnToReturn)
            $this->btnSave->Enabled = true;

        return $blnToReturn;
    }

    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
        try {
            if (!$this->mctService->EditMode)
                $this->mctService->Service->LogInsert = QDateTime::Now();

            $this->mctService->Service->LogUser = $this->objCurrentUser->Username;
            $this->mctService->SaveService();

            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate('Content Saved Successful !'));
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }

        $this->btnSave->Enabled = true;
    }
}

Edit::Run('Edit', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/service/edit.tpl.php');
?>